﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.Car_Rental;
using Entity.Car_Rental;
namespace Background.Controllers
{
    public class ProductPropertyController : Common.Background.Web.Controllers.BackgroundBaseController
    {
        //
        // GET: /ProductProperty/

        IProductPropertyRepository iProductPropertyRepository = null;
        public ProductPropertyController()
        {
            iProductPropertyRepository = new ProductPropertyRepository();
        }

        public ActionResult Index(int? page)
        {
            VP = new Entity.VPredication();
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            var model = iProductPropertyRepository.GetProductProperty(VP, PP);
            return View(model);
        }

        public ActionResult Details(int? id)
        {
            if (id.HasValue)
                return View(iProductPropertyRepository.GetDetailModel().FirstOrDefault(i => i.PropertyID == id.Value));
            return View();
        }
        [HttpPost]
        public ActionResult Details(FormCollection form)
        {
            ProductProperty entity = new ProductProperty();
            TryUpdateModel(entity);
            entity.PropertyTypeID = Convert.ToInt32(form["ProductPropertyTypeID"]);
            if (entity.IsValid)
            {
                if (entity.PropertyID == 0)
                    iProductPropertyRepository.Insert(entity);
                else
                    iProductPropertyRepository.Update(entity);
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View(iProductPropertyRepository.GetDetailModel().FirstOrDefault(i => i.PropertyID == entity.PropertyID));
        }
        public ActionResult Delete(int id)
        {
            iProductPropertyRepository.Delete(new ProductProperty { PropertyID = id });
            return AlertToUrl("Index");
        }

    }
}
